package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.CountryDomain;
import com.xingkeduo.pojo.json.CountryJson;
import com.xingkeduo.pojo.query.CountryQuery;
import com.xingkeduo.pojo.table.CountryTable;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;


/**
 * 模块管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@RestController
@RequestMapping(CMS_URL_PREFIX + "/country")
public class CountryController extends InitController {

    @Autowired
    private CountryDomain countryDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            CountryTable countryVo = JSON.parseObject(urlParam, CountryTable.class);
            CountryTable countryTable = countryDomain.getCountryById(countryVo.getId());
            return returnUtil.getSuccess(CountryJson.toJson(countryTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }


    //修改
    @RequestMapping(value = "/lists/{urlParam}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            CountryQuery countryQuery = JSON.parseObject(urlParam, CountryQuery.class);
            List<CountryTable> countryList = countryDomain.getCountryList(countryQuery);

            return returnUtil.getSuccess(CountryJson.toJson(countryList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    /**
     * 根据name查询城市
     *
     * @return :
     * @author : gaozifeng
     * @date : 2019/4/17 17:00
     */
    @GetMapping("/name")
    public String selectCityId(String name) {
        returnUtil = new ReturnUtil();
        if (null == name) {
            return returnUtil.getException("0", "城市不能为空");
        }
        try {
            CountryTable city = countryDomain.getCityId(name);
            return returnUtil.getSuccess(CountryJson.toJson(city));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
